package com.aaa.book_mall.controller;

import com.aaa.book_mall.dao.BookDao;
import com.aaa.book_mall.dao.DetailDao;
import com.aaa.book_mall.dao.RecordDao;
import com.aaa.book_mall.dao.impl.BookDaoImpl;
import com.aaa.book_mall.dao.impl.DetailDaoImpl;
import com.aaa.book_mall.dao.impl.RecordDaoImpl;
import com.aaa.book_mall.entity.Detail;
import com.aaa.book_mall.entity.Proln;
import com.aaa.book_mall.entity.Record;
import com.aaa.book_mall.entity.Request;
import com.aaa.book_mall.util.LayTab;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * fileName:RecordController
 * description:
 * author:付亚星
 * createTime:2021/1/28 23:25
 * version:1.0.0
 */
@RestController
@RequestMapping("/record")
public class RecordController {

    private DetailDao detailDao = new DetailDaoImpl();
    private BookDao bookDao = new BookDaoImpl();
    private RecordDao recordDao = new RecordDaoImpl();
    @RequestMapping("/add")
    public Map add(@RequestBody Request outs, HttpServletRequest request){
        System.out.println("add方法进来了");
        /*       2、更改t_book对应书本的数量
         *             先根据outnumber到t_outdetail表中查询申请的每一本书
         *             根据查询出来的书的量和书的编号到t_book表中操作
         * */
        List<Detail> outDetails = detailDao.queryDetail(outs.getNumber());//查询订单详情
        if(outDetails!=null && outDetails.size()>0){
            for (Detail outDetail : outDetails) {
                int bookcount = bookDao.selectCountByBookid(outDetail.getBookid());//查询t_book中的书本数量
                bookcount=bookcount+outDetail.getBookcount();//将原有数量加上申请的数量
                //bookDao.updateCountByBookid(bookcount,outDetail.getBookid());//更新t_book中的count
            }
        }
        //        获取现在的时间，和用户便函共同组成outnumber
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
        String format = simpleDateFormat.format(date);
        String stringDate =104+format.replace("-", "").replace(":","");
        //将信息添加到入库信息中
        Record record = new Record(null,stringDate,101,"小楚",outs.getBookcount(),outs.getBookprice(),date);
        int i = recordDao.insertRecord(record);

        HashMap<Object, Object> map = new HashMap<>();
        if(i>0){
            map.put("code","1");
            map.put("msg","操作成功");
        }else{
            map.put("code","0");
            map.put("msg","操作失败");
        }
        return map;
    }

    /**
     * 查询所有t_rukurecord的信息
     */
    @RequestMapping("/getAllRecord")
    public LayTab getAllRecord(Record record){
        System.out.println("查询入库信息");
        //        查询出来目标数据
        List<Map<String, Object>> allRecord = recordDao.getAllRecord(record);
        //        查询出来所有的总条数
        int count = recordDao.queryCountAllInfo();
        LayTab layTab = new LayTab(count, allRecord);
        return layTab;
    }

}
